package com.example.li.view;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.li.ACache;
import com.example.li.bean.PersonInfo;
import com.example.li.fllowstep.R;
import com.example.li.view.base.BaseActivity;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtMobile;
    private EditText mEtPwd;
    private Button mBtnLogin;
    private TextView tvRegist;
    private TextView tvForgetPwd;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initData();
        addListener();
    }


    private void addListener() {
        mBtnLogin.setOnClickListener(this);
        tvRegist.setOnClickListener(this);
        tvForgetPwd.setOnClickListener(this);
    }

    private void initView() {
        mEtMobile = (EditText) findViewById(R.id.et_mobile);
        mEtPwd = (EditText) findViewById(R.id.et_pwd);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        tvRegist = (TextView) findViewById(R.id.tv_regist);
        tvForgetPwd = (TextView) findViewById(R.id.tv_forget_pwd);
    }

    private void initData() {
        ACache aCache = ACache.get(getApplicationContext());
        String mobile = aCache.getAsString("username");
        String pwd = aCache.getAsString("password");
        if (!TextUtils.isEmpty(mobile) && !TextUtils.isEmpty(pwd)) {
            mEtMobile.setText(mobile);
            mEtMobile.setSelection(mobile.length());
            mEtPwd.setText(pwd);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                String mobile = mEtMobile.getText().toString().trim();
                String pwd = mEtPwd.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                doLogin(mobile, pwd);
                break;
            case R.id.tv_regist:
                Intent mIntent = new Intent();
                mIntent.setClass(this, RegistActivity.class);
                int requestCode = 0;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.tv_forget_pwd:
//                startActivity(new Intent(this, ReSetPwdActivity.class));
                break;
        }
    }


    private void doLogin(final String mobile, String pwd) {
        showProgressDialog(false);
        PersonInfo user = new PersonInfo();
        user.setUsername(mobile);
        user.setPassword(pwd);
        user.login(new SaveListener<PersonInfo>() {
            @Override
            public void done(PersonInfo user, BmobException e) {
                stopProgressDialog();
                if (e == null) {

                    PersonInfo pi = PersonInfo.getCurrentUser(PersonInfo.class);
                    if (null != pi) {
                        pi.setMobile(mobile);
//                        DBHandler.insertPesonInfo(pi);
                    }
                    ACache aCache = ACache.get(getApplicationContext());
                    //存储
                    if(TextUtils.isEmpty(aCache.getAsString("username"))){
                        aCache.put("username",mobile);
                        aCache.put("password",pwd);
                    }
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    LoginActivity.this.finish();
                } else {
                    toast("登录失败:" + e.getMessage());
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(data == null){
            return;
        }

        String username = data.getStringExtra("username");
        String password = data.getStringExtra("password");
        switch (requestCode) {
            case 0:
                mEtMobile.setText(username);
                mEtPwd.setText(password);
                break;
            default:
                break;
        }
    }
}
